#include <CMPackSim.h>
#include <UIMPackSim.h>
#include <BMPackSim.h>
#include <corelib\tools\qrect.h>
#include <gui\math3d\qvector2d.h>



#include <conveyor.h>
#include <ellipseitem.h>
#include <ItemCreator.h>

#include <gui\painting\qtransform.h>

#include <QRectF>
#include <gui\graphicsview\qgraphicsitem.h>




namespace PackSim
{


	CMPackSim::CMPackSim()
	{
		mUIMPackSim = NULL;
		mBMPackSim = new BMPackSim(this);
	}

	CMPackSim::~CMPackSim()
	{

	}

	QMainWindow* CMPackSim::getUI()
	{

		if (!mUIMPackSim)
		{
			mUIMPackSim =new UIMPackSim();
			mUIMPackSim->setScene(mBMPackSim->scene());
			QObject::connect(dynamic_cast<QObject*>(mUIMPackSim), SIGNAL(PickGuiOnStartClicked()),    this, SLOT(onPickGuiOnStartClicked()));
			QObject::connect(dynamic_cast<QObject*>(mUIMPackSim), SIGNAL(PickGuiOnStopClicked()),     this, SLOT(onPickGuiOnStopClicked()));
			QObject::connect(dynamic_cast<QObject*>(mUIMPackSim), SIGNAL(PickGuiOnStartCnvClicked()), this, SLOT(onPickGuiOnStartCnvClicked()));
			QObject::connect(dynamic_cast<QObject*>(mUIMPackSim), SIGNAL(PickGuiOnStopCnvClicked()),  this, SLOT(onPickGuiOnStopCnvClicked()));
			QObject::connect(dynamic_cast<QObject*>(mUIMPackSim), SIGNAL(PickGuiOnStartCRTClicked()),  this, SLOT(onPickGuiOnStartCRTClicked()));
			QObject::connect(dynamic_cast<QObject*>(mUIMPackSim), SIGNAL(PickGuiOnStopCRTClicked()),  this, SLOT(onPickGuiOnStopCRTClicked()));
		}

		return dynamic_cast<QMainWindow*>(mUIMPackSim);
	}



	void CMPackSim::loadItems()
	{
		mBMPackSim->createConveyor(QString("CNV0"));
		mBMPackSim->getConveyor(QString("CNV0"))->setAcceleration(6.0F);
		mBMPackSim->getConveyor(QString("CNV0"))->setRotation(30);
		mBMPackSim->getConveyor(QString("CNV0"))->setPos(450,315);
		mBMPackSim->getConveyor(QString("CNV0"))->setRect(QRectF(0,0,400,50));
		mBMPackSim->getConveyor(QString("CNV0"))->setVelocity(15.0F);
		mBMPackSim->getConveyor(QString("CNV0"))->setColor(QColor(Qt::green));

		mBMPackSim->createConveyor(QString("CNV1"));
		mBMPackSim->getConveyor(QString("CNV1"))->setAcceleration(6.0F);
		mBMPackSim->getConveyor(QString("CNV1"))->setRotation(-30);
		mBMPackSim->getConveyor(QString("CNV1"))->setPos(410,265);
		mBMPackSim->getConveyor(QString("CNV1"))->setRect(QRectF(0,0,400,50));
		mBMPackSim->getConveyor(QString("CNV1"))->setVelocity(15.0F);
		mBMPackSim->getConveyor(QString("CNV1"))->setColor(QColor(Qt::green));


		mBMPackSim->createConveyor(QString("CNV2"));
		mBMPackSim->getConveyor(QString("CNV2"))->setAcceleration(15.0F);
		mBMPackSim->getConveyor(QString("CNV2"))->setRotation(0);
		mBMPackSim->getConveyor(QString("CNV2"))->setPos(50,250);
		mBMPackSim->getConveyor(QString("CNV2"))->setRect(QRectF(0,0,400,150));
		mBMPackSim->getConveyor(QString("CNV2"))->setVelocity(45.0F);
		mBMPackSim->getConveyor(QString("CNV2"))->setColor(QColor(Qt::red));

		QObject* cnvObj = mBMPackSim->getConveyor(QString("CNV0"));		
		bool test = QObject::connect(cnvObj,SIGNAL(ConveyorSignalItemEndOfCnv(Conveyor*, EllipseItem* )),this,SLOT(onConveyorSignalItemEndOfCnv(Conveyor*, EllipseItem*)));

		cnvObj = mBMPackSim->getConveyor(QString("CNV1"));		
		test = QObject::connect(cnvObj,SIGNAL(ConveyorSignalItemEndOfCnv(Conveyor*, EllipseItem* )),this,SLOT(onConveyorSignalItemEndOfCnv(Conveyor*, EllipseItem*)));
		
		cnvObj = mBMPackSim->getConveyor(QString("CNV2"));
		test = QObject::connect(cnvObj,SIGNAL(ConveyorSignalItemEndOfCnv(Conveyor*, EllipseItem* )),this,SLOT(onConveyorSignalItemEndOfCnv(Conveyor*, EllipseItem*)));

		mBMPackSim->getConveyor(QString("CNV2"))->appendNextConveyor(mBMPackSim->getConveyor(QString("CNV1")));
		mBMPackSim->getConveyor(QString("CNV2"))->appendNextConveyor(mBMPackSim->getConveyor(QString("CNV0")));

		EllipseItem* itm;

		//itm = mBMPackSim->createEllipseItem( mBMPackSim->getConveyor(QString("CNV2")));
		//itm->setPos(5,20);
		//itm->setColor(QColor(Qt::cyan));
		//itm->setRect(-5,-10,10,20);
		//itm->setRotation(-45);


		//itm = mBMPackSim->createEllipseItem( mBMPackSim->getConveyor(QString("CNV2")));
		//itm->setPos(5,40);
		//itm->setColor(QColor(Qt::cyan));
		//itm->setRect(-5,-10,10,20);
		//itm->setRotation(-45);


		//itm = mBMPackSim->createEllipseItem( mBMPackSim->getConveyor(QString("CNV2")));
		//itm->setPos(30,20);
		//itm->setColor(QColor(Qt::cyan));
		//itm->setRect(-5,-10,10,20);
		//itm->setRotation(-15);


		//itm = mBMPackSim->createEllipseItem( mBMPackSim->getConveyor(QString("CNV2")));
		//itm->setPos(30,40);
		//itm->setColor(QColor(Qt::cyan));
		//itm->setRect(-5,-10,10,20);
		//itm->setRotation(-15);

		//itm = mBMPackSim->createEllipseItem( mBMPackSim->getConveyor(QString("CNV2")));
		//itm->setPos(5,60);
		//itm->setColor(QColor(Qt::cyan));
		//itm->setRect(-5,-10,10,20);
		//itm->setRotation(-45);


		//itm = mBMPackSim->createEllipseItem( mBMPackSim->getConveyor(QString("CNV2")));
		//itm->setPos(5,80);
		//itm->setColor(QColor(Qt::cyan));
		//itm->setRect(-5,-10,10,20);
		//itm->setRotation(-45);


		//itm = mBMPackSim->createEllipseItem( mBMPackSim->getConveyor(QString("CNV2")));
		//itm->setPos(30,60);
		//itm->setColor(QColor(Qt::cyan));
		//itm->setRect(-5,-10,10,20);
		//itm->setRotation(-15);


		//itm = mBMPackSim->createEllipseItem( mBMPackSim->getConveyor(QString("CNV2")));
		//itm->setPos(30,80);
		//itm->setColor(QColor(Qt::cyan));
		//itm->setRect(-5,-10,10,20);
		//itm->setRotation(-15);


		ItemCreator* crt0 = mBMPackSim->createItemCreator(QString("CRT0"));
		crt0->setPos(100,250);
		crt0->setColumNumber(1);
		crt0->setCreationPeriod(1000);

		QObject* crtObj = dynamic_cast<QObject*>(crt0);

		test = QObject::connect(crtObj,SIGNAL(ItemCreatorSignalItemsCreated(ItemCreator*, QList<EllipseItem*>&)),this,SLOT(onItemCreatorSignalItemsCreated(ItemCreator*, QList<EllipseItem*>&)));


		



	}


	void CMPackSim::onPickGuiOnStartClicked()
	{
		mBMPackSim->startSimulation();
	}
	void CMPackSim::onPickGuiOnStopClicked()
	{
		mBMPackSim->stopSimulation();
	}

	void CMPackSim::onPickGuiOnStartCnvClicked()
	{
		Conveyor* cnv = mBMPackSim->getConveyor(QString("CNV0"));
		cnv->startCnv();

	    cnv = mBMPackSim->getConveyor(QString("CNV1"));
		cnv->startCnv();

		cnv = mBMPackSim->getConveyor(QString("CNV2"));
		cnv->startCnv();
	}

	void CMPackSim::onPickGuiOnStopCnvClicked()
	{
		Conveyor* cnv = mBMPackSim->getConveyor(QString("CNV0"));
		cnv->stopCnv();

		cnv = mBMPackSim->getConveyor(QString("CNV1"));
		cnv->stopCnv();

		cnv = mBMPackSim->getConveyor(QString("CNV2"));
		cnv->stopCnv();
	}

	void CMPackSim::onPickGuiOnStartCRTClicked()
	{
		ItemCreator* crt = mBMPackSim->getItemCreator(QString("CRT0"));
		crt->startCreation();
	}

	void CMPackSim::onPickGuiOnStopCRTClicked()
	{
		ItemCreator* crt = mBMPackSim->getItemCreator(QString("CRT0"));
		crt->stopCreation();
	}


	void CMPackSim::onConveyorSignalItemEndOfCnv( Conveyor* aConveyor, EllipseItem* aItem )
	{
		QList<Conveyor*> cnvList = aConveyor->nextConveyorList();
		if(cnvList.isEmpty())
		{
			mBMPackSim->destroyEllipseItem(aItem);
		}
		else 
		{		
			QPointF pos = aItem->pos();
			pos.setX(aConveyor->rect().width());
			aItem->setPos(pos);
			QPointF pTemp = aItem->mapToScene(QPointF(0,0));

			QPointF position;
			foreach(Conveyor* cnv,cnvList)
			{
				position = cnv->mapFromScene(pTemp);

				if(cnv->contains(position))
				{
					double rotation = aItem->rotation() + aConveyor->rotation()- cnv->rotation();
					EllipseItem* itm = mBMPackSim->createEllipseItem( cnv);
					itm->setPos(position);
					itm->setColor(aItem->color());
					itm->setRect(aItem->rect());
					itm->setRotation(rotation);
				}
			}
			
			mBMPackSim->destroyEllipseItem(aItem);		

		}

		
	}

	void CMPackSim::onItemCreatorSignalItemsCreated(ItemCreator* aItemCreator, QList<EllipseItem*>& aItemList)
	{
		QPointF tmpPos;
		QPointF mappedPos;

		
		foreach(EllipseItem* creatorItem,aItemList)
		{
			foreach(Conveyor* cnv, mBMPackSim->conveyorMap())
			{
				tmpPos = creatorItem->mapToScene(QPointF(0,0));
				mappedPos = cnv->mapFromScene(tmpPos);
				if(cnv->contains(mappedPos))
				{
					double rotation = creatorItem->rotation() + aItemCreator->rotation() - cnv->rotation();
					EllipseItem* itm = mBMPackSim->createEllipseItem(cnv);
					itm->setRotation(rotation);
					itm->setPos(mappedPos);
					itm->setColor(creatorItem->color());
					itm->setRect(creatorItem->rect());

					break;
				}
			}
			delete creatorItem;
		}
	}

}



